
package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.common.entity.CommonType;
import com.joysuch.wwyt.common.repository.CommonTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 试题类型
 */
@Component
public class ImportQuestionTypeIdConvertor implements FieldValueConvert<String, Long> {
    @Autowired
    private CommonTypeDao commonTypeDao;
    @Override
    public Long convert(Object bean, String value, DataDumpField dataField) {
        List<CommonType> types = commonTypeDao.findQuestionTypeNames(value);
        if (CollectionUtils.isEmpty(types)) {
            CommonType save = new CommonType();
            save.setType("试题");
            save.setName(value);
            save.setObjCount(1);
            commonTypeDao.save(save);
            return save.getId();
        }else {
            if (types.size() == 1) {
                CommonType type = types.get(0);
                Integer count = type.getObjCount();
                type.setObjCount(count == null ? 1 : count++);
                commonTypeDao.save(type);
                return type.getId();
            }else {
                throw new IllegalArgumentException("多个对象");
            }
        }
    }
}
